require 'digest/sha1'

class User < ActiveRecord::Base
  include Authentication
  include Authentication::ByPassword
  include Authentication::ByCookieToken

  validates_presence_of     :login
  validates_length_of       :login,    :within => 3..40
  validates_uniqueness_of   :login
  validates_format_of       :login,    :with => Authentication.login_regex, :message => Authentication.bad_login_message

  validates_format_of       :name,     :with => Authentication.name_regex,  :message => Authentication.bad_name_message, :allow_nil => true
  validates_length_of       :name,     :maximum => 100

  validates_presence_of     :email
  validates_length_of       :email,    :within => 6..100 #r@a.wk
  validates_uniqueness_of   :email
  validates_format_of       :email,    :with => Authentication.email_regex, :message => Authentication.bad_email_message
  validates_presence_of :salary
  validates_presence_of :hours
  validates_presence_of :department_id
  validates_presence_of :branch_office_id

  belongs_to :department
  belongs_to :branch_office
  has_many :offs
  has_many :punches, :class_name => 'Hour'
  has_many :missing_hours
  has_many :hour_banks

  before_create :make_activation_code

  attr_accessible :login, :email, :name, :password, :password_confirmation, :salary, :hours, :department_id, :branch_office_id, :genre

  def get_name
    name || login
  end

  def is_administrator?
    is_admin == true
  end

  def off_hours(month=Time.now.month, year=Time.now.year)
    User.connection.execute "CALL off_hours(#{self.id}, #{month}, #{year}, @result)"
    User.connection.execute("SELECT float2hour(@result) AS result").fetch_row.first.to_s
  end

  def expected_hours(month=Time.now.month, year=Time.now.year)
    User.connection.execute "CALL user_hours(#{self.id}, #{month}, #{year}, @result)"
    User.connection.execute("SELECT float2hour(@result) AS result").fetch_row.first.to_s
  end

  def worked_hours(month=Time.now.month, year=Time.now.year)
    User.connection.execute "CALL worked_hours(#{self.id}, #{month}, #{year}, @result)"
    User.connection.execute("SELECT float2hour(@result) AS result").fetch_row.first || '0h0min'
  end

  def ever_worked?
    !punches.blank?
  end

  def punch_in(ip)
    Hour.create! :ip => ip, :user_id => self.id, :in_or_out => 'in'
  end

  def punch_out(ip)
    Hour.create! :ip => ip, :user_id => self.id, :in_or_out => 'out'
  end

  def punched_in?
    ever_worked? and self.punches.first(:order => 'created_at DESC').in_or_out == 'in'
  end

  def total_hour_banks
    HourBank.find(:all, :select => "float2hour(SUM(hours)) AS amount", :conditions => { :user_id => self.id })[0].amount || '0h0min'
  end

  def activate!
    @activated = true
    self.activated_at = Time.now.utc
    self.activation_code = nil
    save(false)
  end

  def recently_activated?
    @activated
  end

  def active?
    activation_code.nil?
  end

  def self.authenticate(login, password)
    return nil if login.blank? || password.blank?
    u = find :first, :conditions => ['login = ? and activated_at IS NOT NULL', login] # need to get the salt
    u && u.authenticated?(password) ? u : nil
  end

  def login=(value)
    write_attribute :login, (value ? value.downcase : nil)
  end

  def email=(value)
    write_attribute :email, (value ? value.downcase : nil)
  end

  protected
    
  def make_activation_code
      self.activation_code = self.class.make_token
  end

end
